//
//  GGUserInfoViewController.m
//  cherryproduct
//
//  Created by zou shigang on 14/8/30.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "GGUserInfoViewController.h"
#import "ChangViewController.h"
#import "babyMessageViewController.h"
#import "SMImagePickerController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "ChangPokViewController.h"

@interface GGUserInfoViewController ()<UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate, UIAlertViewDelegate>
{
    UITapGestureRecognizer * _tap;
    
    NSArray * array_title_0;
    NSArray * array_title_1;
    
    NSMutableDictionary * data_list;
    
    int _month;
    int _year;
    
    ALAssetsLibrary * assetsLibrary_for_url;
    
    NSInteger select_index_picker;
}

@property(nonatomic, strong) UITableView * tableview;

@property(nonatomic, strong) UIImageView * imgProfile;
@property(nonatomic, strong) UIImageView * img_header;

@property(nonatomic, strong) NSString * str_update_url;

@end

@implementation GGUserInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    array_title_0=[[NSArray alloc] initWithObjects:@"昵称",@"签名",@"宝宝信息",@"消息提醒时间", nil];
    array_title_1=[[NSArray alloc] initWithObjects:@"版本",@"意见反馈", nil];
    // Do any additional setup after loading the view.
    self.myView.hidden = YES;
    [self setNavigationBarColor:[UIColor clearColor]];
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat width = self.view.frame.size.width;
    if (self.imgProfile==nil) {
        self.imgProfile = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, 164)];
        self.imgProfile.contentMode=UIViewContentModeScaleAspectFill;
        self.imgProfile.clipsToBounds=YES;
        [self.imgProfile setImage:[UIImage imageNamed:@"user_bg"]];
        [self.view addSubview:self.imgProfile];
    }
    
    if (self.tableview==nil) {
        self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        self.tableview.backgroundColor=[UIColor clearColor];
        self.tableview.dataSource=self;
        self.tableview.delegate=self;
        self.tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.tableview.showsVerticalScrollIndicator=NO;
        
        UIView * view_header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 164)];
        view_header.backgroundColor=[UIColor clearColor];
        {
            UIButton * btn_header = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-35, 60, 70, 70)];
            btn_header.backgroundColor=[UIColor clearColor];
            [btn_header addTarget:self action:@selector(doActionHeader:) forControlEvents:UIControlEventTouchUpInside];
            
            self.img_header = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
            self.img_header.userInteractionEnabled = YES;
            self.img_header.image = [UIImage imageNamed:@"bodyheader"];
            self.img_header.userInteractionEnabled=NO;
            
            self.img_header.layer.cornerRadius = 35;
            self.img_header.layer.masksToBounds = YES;
            self.img_header.layer.borderWidth = 3.0;
            self.img_header.layer.borderColor = [UIColor whiteColor].CGColor;
            
            [btn_header addSubview:self.img_header];
            
            [view_header addSubview:btn_header];
            
            //tap
            _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doActionBG:)];
            [view_header addGestureRecognizer:_tap];
        }
        self.tableview.tableHeaderView=view_header;
        
        [self.view addSubview:self.tableview];
    }
    {
        //设置图片
        //保存login img
        NSString * new_logo = [[LoginViewController shareUserInfo] objectForKey:@"new_logo"];
        NSString * new_cover = [[LoginViewController shareUserInfo] objectForKey:@"new_cover"];
        if (new_logo) {
            [self.img_header setImageWithURL:[NSURL URLWithString:new_logo]
                            placeholderImage:[UIImage imageNamed:@"bodyheader"]];
        }
        if (new_cover) {
            [self.imgProfile setImageWithURL:[NSURL URLWithString:new_cover]
                            placeholderImage:[UIImage imageNamed:@"user_bg"]];
        }
    }
    
    {
        UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, IOS7_OR_LATER?20:0, [UIImage imageNamed:@"topBack.png"].size.width-5, 44)];
        if (IOS7_OR_LATER) {
            [backButton setImage:[UIImage imageNamed:@"topBack.png"] forState:UIControlStateNormal];
            [backButton setImage:[UIImage imageNamed:@"topBack.png"] forState:UIControlStateHighlighted];
        } else {
            [backButton setBackgroundImage:[UIImage imageNamed:@"topBack.png"] forState:UIControlStateNormal];
            [backButton setBackgroundImage:[UIImage imageNamed:@"topBack.png"] forState:UIControlStateHighlighted];
        }
        [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:backButton];
    }
    
    //getdata
    [self getUserData];
}

- (void)doActionHeader:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"替换头像"
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:@"拍照"
                                                    otherButtonTitles:@"相册",nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
    
    select_index_picker = 1;
}
- (void)doActionBG:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"替换背景"
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:@"拍照"
                                                    otherButtonTitles:@"相册",nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];
    
    select_index_picker = 2;
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if (buttonIndex==2) {
        return;
    }
    if (buttonIndex==0) {
        UIImagePickerControllerSourceType type;
        type = UIImagePickerControllerSourceTypeCamera;
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            type=UIImagePickerControllerSourceTypeCamera;
        } else {
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"该设备不支持拍照" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            return;
        }
        
        UIImagePickerController * pickerImage = [[UIImagePickerController alloc] init];
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            pickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
            pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
        }
        pickerImage.delegate = self;
        pickerImage.allowsEditing = YES;
        [self presentViewController:pickerImage animated:YES completion:nil];
        return;
    }
    if (buttonIndex==1) {
        UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
        }
        pickerImage.delegate = self;
        pickerImage.allowsEditing = YES;
        [self presentViewController:pickerImage animated:YES completion:nil];
        return;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark MarkHeader

static CGFloat ImageHeight  = 164.f;
//static CGFloat ImageWidth  = 320.0;

- (void)updateImg {
    CGFloat yOffset   = self.tableview.contentOffset.y;
    CGFloat f_height = 0;
    
    CGFloat ImageWidth = self.view.frame.size.width;
    /*
    if ([[UIDevice currentDevice].systemVersion doubleValue] < 7.0) {
        f_height=20;
    }*/
    
    if (yOffset < 0) {
        CGFloat factor = ((ABS(yOffset)+ImageHeight)*ImageWidth)/ImageHeight;
        CGRect f = CGRectMake(-(factor-ImageWidth)/2, f_height, factor, ImageHeight+ABS(yOffset));
        self.imgProfile.frame = f;
    } else {
        CGRect f = self.imgProfile.frame;
        f.origin.y = f_height-yOffset;
        self.imgProfile.frame = f;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self updateImg];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)DoMoreThings
{
    if (data_list) {
        NSString * baby_sex = [data_list objectForKey:@"baby_sex"];
        NSMutableString * str_info = [[NSMutableString alloc] initWithCapacity:40];
        if (baby_sex && ![baby_sex isKindOfClass:[NSNull class]]) {
            if ([baby_sex isEqualToString:@"0"]) {
                [str_info appendString:@"女  "];
            }
            if ([baby_sex isEqualToString:@"1"]) {
                [str_info appendString:@"男  "];
            }
        } else {
            [str_info appendString:@"没有选择"];
        }
        
        _year = 0;
        _month = 0;
        
        NSString * baby_birthday = [data_list objectForKey:@"baby_birthday"];
        if (baby_birthday && ![baby_birthday isKindOfClass:[NSNull class]] && ![baby_birthday isEqualToString:@""]) {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"yyyy-MM-dd"];
            NSDate *date1=[formatter dateFromString:baby_birthday];
            
            NSDate *now=[NSDate date];
            //日期减
            NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
            unsigned int unitFlags = NSDayCalendarUnit;
            NSDateComponents *comps = [gregorian components:unitFlags fromDate:date1  toDate:now  options:0];
            int days = [comps day];
            _year = days/365;
            _month = days%365/30;
        }
        //...
        [str_info appendFormat:@"%d岁零%d个月", _year, _month];
        [data_list setObject:str_info forKey:@"new_boby_info"];
    }
}

#pragma mark -
#pragma mark UITableViewDelegate UITableViewDataSource
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    if (indexPath.section==0) {
        
        if (indexPath.row==0) {
            ChangViewController* vc = [[ChangViewController alloc] init];
            [vc setLabel:nil title:@"昵称"];
            vc.block_dismiss = ^(NSString * str) {
                if (data_list) {
                    [data_list setObject:str forKey:@"nickname"];
                    [self.tableview reloadData];
                }
            };
            {
                NSString * nickname = [data_list objectForKey:@"nickname"];
                if (nickname && [nickname isKindOfClass:[NSString class]]) {
                    vc.str_show_info = nickname;
                }
            }
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row==1) {
            ChangViewController* vc = [[ChangViewController alloc] init];
            [vc setLabel:nil title:@"签名"];
            vc.block_dismiss = ^(NSString * str) {
                if (data_list) {
                    [data_list setObject:str forKey:@"sign"];
                    [self.tableview reloadData];
                }
            };
            {
                NSString * sign = [data_list objectForKey:@"sign"];
                if (sign && [sign isKindOfClass:[NSString class]]) {
                    vc.str_show_info = sign;
                }
            }
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row==2) {
            babyMessageViewController* vc = [[babyMessageViewController alloc] init];
            vc.str_boday = [data_list objectForKey:@"baby_birthday"];
            
            NSString * baby_sex = [data_list objectForKey:@"baby_sex"];
            if (baby_sex) {
                vc.str_sex_info=baby_sex;
            } else {
                vc.str_sex_info=@"1";
            }
            vc.block_dismiss = ^(NSString * info, NSString * sex, NSString * str_birthday) {
                if (data_list) {
                    [data_list setObject:info forKey:@"new_boby_info"];
                    [data_list setObject:sex forKey:@"baby_sex"];
                    [data_list setObject:str_birthday forKey:@"baby_birthday"];
                    
                    [self.tableview reloadData];
                }
            };
            [vc setYearAndmonth:_year month:_month babyLabel:nil];
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row==3) {
            /*
            ChangViewController * vc = [[ChangViewController alloc] init];
            [vc setLabel:nil title:@"消息提醒时间"];
            vc.block_dismiss = ^(NSString * str) {
                if (data_list) {
                    [data_list setObject:str forKey:@"warning"];
                    [self.tableview reloadData];
                }
            };
            [self.navigationController pushViewController:vc animated:YES];
             */
            ChangPokViewController * vc = [[ChangPokViewController alloc] init];
            vc.block_dismiss = ^(NSString * str) {
                if (data_list) {
                    [data_list setObject:str forKey:@"warning"];
                    [self.tableview reloadData];
                }
            };
            NSString * warning = [data_list objectForKey:@"warning"];
            if (warning && [warning isKindOfClass:[NSString class]]) {
                if ([warning isEqualToString:@"10"]) {
                    vc.select_index=1;
                }
                if ([warning isEqualToString:@"30"]) {
                    vc.select_index=2;
                }
                if ([warning isEqualToString:@"60"]) {
                    vc.select_index=3;
                }
            }
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    if (indexPath.section==1) {
        if (indexPath.row==1) {
            ChangViewController* vc = [[ChangViewController alloc] init];
            [vc setLabel:nil title:@"意见反馈"];
            [self.navigationController pushViewController:vc animated:YES];
        }
        if (indexPath.row==0) {
            [self getVersion];
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    if (section==0) {
        return 4;
    }
    if (section==1) {
        return 2;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString * str_cell = @"cell";
    
    SETableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str_cell];
    if (cell == nil) {
        cell = [[SETableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str_cell];
    }
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
    NSInteger section = indexPath.section;
    NSInteger row_index = indexPath.row;
    
    cell.text_info.text=@"";
    
    if (section==0) {
        if (row_index < [array_title_0 count]) {
            cell.label_title.text=[array_title_0 objectAtIndex:row_index];
        }
        if (data_list) {
            if (row_index==0) {
                NSString * nickname = [data_list objectForKey:@"nickname"];
                if (nickname && [nickname isKindOfClass:[NSString class]]) {
                    cell.text_info.text = nickname;
                }
            }
            if (row_index==1) {
                NSString * sign = [data_list objectForKey:@"sign"];
                if (sign && [sign isKindOfClass:[NSString class]]) {
                    cell.text_info.text = sign;
                }
            }
            if (row_index==2) {
                NSString * new_boby_info = [data_list objectForKey:@"new_boby_info"];
                if (new_boby_info && [new_boby_info isKindOfClass:[NSString class]]) {
                    cell.text_info.text = new_boby_info;
                }
            }
            if (row_index==3) {
                NSString * warning = [data_list objectForKey:@"warning"];
                if (warning && [warning isKindOfClass:[NSString class]]) {
                    cell.text_info.text=warning;
                }
            }
        }
    }
    if (section==1) {
        if (row_index < [array_title_1 count]) {
            cell.label_title.text=[array_title_1 objectAtIndex:row_index];
        }
        
        if (row_index==0) {
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            if (app_Version) {
                cell.text_info.text=app_Version;
            }
        }
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
    return 55;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
{
    if (section == 0) {
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)];
        view.backgroundColor=[UIColor whiteColor];
        
        UILabel * label_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 200, 30)];
        label_title.backgroundColor=[UIColor clearColor];
        label_title.font=[UIFont systemFontOfSize:15];
        label_title.textColor=RGBCOLOR(80, 170, 248);
        label_title.text=@"关于我们";
        [view addSubview:label_title];
        
        return view;
    }
    if (section == 1) {
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)];
        view.backgroundColor=[UIColor whiteColor];
        
        CGFloat width = self.view.frame.size.width;
        
        UIButton * btn_login_out = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, width-20, 35)];
        btn_login_out.layer.borderColor  = RGBCOLOR(250, 110, 110).CGColor;
        btn_login_out.layer.borderWidth  = 1.0;
        btn_login_out.layer.cornerRadius = 5.0;
        btn_login_out.backgroundColor=RGBCOLOR(250, 110, 110);
        
        UILabel * label_right = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width-20, 35)];
        label_right.backgroundColor=[UIColor clearColor];
        label_right.text=@"注销";
        label_right.font=[UIFont systemFontOfSize:16];
        label_right.textColor=[UIColor whiteColor];
        label_right.textAlignment=NSTextAlignmentCenter;
        
        [btn_login_out addSubview:label_right];
        
        [btn_login_out addTarget:self action:@selector(doLoginOut:) forControlEvents:UIControlEventTouchUpInside];
        
        [view addSubview:btn_login_out];
        
        return view;
    }
    return nil;
}

- (void)doLoginOut:(id)sender
{
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    [defaults setObject:@"" forKey:@"USERNAME"];
//    [defaults setObject:@"" forKey:@"USERPASSWORD"];
//    //zsg
//    [[LoginViewController shareUserInfo] setObject:@"0" forKey:KEY_HAVE_LOGIN];
//    
//    [self popToViewController:@"CPHomeViewController"];
    [self goDateLogout];
}

- (void)cleanCookidPP
{
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *_tmpArray = [NSArray arrayWithArray:[cookieJar cookies]];
    for (id obj in _tmpArray) {
        [cookieJar deleteCookie:obj];
    }
    
    //[[NSUserDefaults standardUserDefaults] setObject:data forKey:kUserDefaultsCookie];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kUserDefaultsCookie];
}

- (void)goDateLogout
{
    [self.view startLoadingAnimation];
    
    [[HttpNewBaseService sharedClient] getRequestOperation:@"member/login_out" paramDictionary:nil onCompletion:^(NSString *responseString) {
        [self.view stopLoadingAnimation];
        
        NSDictionary * dic = [responseString JSONValue];
        if (dic && [dic isKindOfClass:[NSDictionary class]]) {
            NSInteger status = [[dic objectForKey:@"status"] integerValue];
            if (status == 1) {
                [[LoginViewController shareUserInfo] setObject:@"0" forKey:KEY_HAVE_LOGIN];
                [LoginViewController saveUserInfo];
                [self cleanCookidPP];
                [self popToViewController:@"CPHomeViewController"];
            } else {
                [self showAlertView:@"温馨提示" subtitle:[dic objectForKey:@"message"]];
            }
        }
        
    } onError:^(NSError *error) {
        [self.view stopLoadingAnimation];
        
        [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
        
        [[LoginViewController shareUserInfo] setObject:@"0" forKey:KEY_HAVE_LOGIN];
        [LoginViewController saveUserInfo];
        [self cleanCookidPP];
        [self popToViewController:@"CPHomeViewController"];
    } onAnimated:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if (buttonIndex == 0) {
        UIApplication *application = [UIApplication sharedApplication];
        [application openURL:[NSURL URLWithString:self.str_update_url]];
    } else {
        return;
    }
}

- (void)getVersion
{
    [self.view startLoadingAnimation];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithCapacity:2];
    [dic setObject:@"1" forKey:@"device_type"];
    [dic setObject:app_Version forKey:@"version"];
    
    [[HttpNewBaseService sharedClient] getRequestOperation:@"member/version_check"
                                           paramDictionary:nil
                                              onCompletion:^(NSString *responseString)
     {
         [self.view stopLoadingAnimation];
         //...
         NSDictionary * dic_res = [responseString JSONValue];
         if (dic_res && [dic_res isKindOfClass:[NSDictionary class]]) {
             
             NSInteger status = [[dic_res objectForKey:@"status"] integerValue];
             if (status == 1) {//有更新
                 NSDictionary * dic_data = [dic_res objectForKey:@"data"];
                 NSInteger force = [[dic_data objectForKey:@"force"] integerValue];
                 NSString * str_message = @"";
                 if ([dic_data objectForKey:@"change"] &&
                     [[dic_data objectForKey:@"change"] isKindOfClass:[NSString class]]) {
                     str_message = [dic_data objectForKey:@"change"];
                 };
                 
                 if ([dic_data objectForKey:@"url"] &&
                     [[dic_data objectForKey:@"url"] isKindOfClass:[NSString class]]) {
                     self.str_update_url = [dic_data objectForKey:@"url"];
                 };
                 
                 if (force == 1) {
                     //强制更新
                     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"软件更新"
                                                                      message:str_message
                                                                     delegate:self
                                                            cancelButtonTitle:@"确定"
                                                            otherButtonTitles:nil, nil];
                     [alert show];
                 } else {
                     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"软件更新"
                                                                      message:str_message
                                                                     delegate:self
                                                            cancelButtonTitle:@"确定"
                                                            otherButtonTitles:@"取消", nil];
                     [alert show];
                 }
             } else {
                 [self showAlertView:@"温馨提示" subtitle:[dic_res objectForKey:@"message"]];
             }
             
         } else {
             [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
         }
         
     } onError:^(NSError *error) {
         [self.view stopLoadingAnimation];
         //...
         [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
     } onAnimated:YES];
}

#pragma mark -
#pragma mark GetData
- (void)getUserData
{
    [self.view startLoadingAnimation];
    
    [[HttpNewBaseService sharedClient] getRequestOperation:@"member/get_profile"
                                           paramDictionary:nil
                                              onCompletion:^(NSString *responseString)
     {
         
         [self.view stopLoadingAnimation];
        @try {
            NSDictionary * dic = [[NSDictionary alloc] initWithDictionary:[responseString JSONValue] copyItems:YES];
            NSInteger status = [[dic objectForKey:@"status"] integerValue];
            if (status == 1) {
                data_list = [[NSMutableDictionary alloc] initWithDictionary:[dic objectForKey:@"data"] copyItems:YES];
                
                if (data_list) {
                    [self DoMoreThings];
                    
                    NSString * logo = [data_list objectForKey:@"logo"];
                    if (logo) {
                        NSString * new_logo = [NSString stringWithFormat:@"%@%@",JPG,logo];
                        
                        [self.img_header setImageWithURL:[NSURL URLWithString:new_logo]
                                        placeholderImage:[UIImage imageNamed:@"bodyheader"]];
                        
                        //保存login img
                        [[LoginViewController shareUserInfo] setObject:new_logo forKey:@"new_logo"];
                    }
                    
                    NSString * cover = [data_list objectForKey:@"cover"];
                    if (cover) {
                        NSString * pp = [cover stringByReplacingOccurrencesOfString:@"Attachment/" withString:@""];
                        NSString * new_cover = [NSString stringWithFormat:@"%@%@",JPG,pp];
                        [self.imgProfile setImageWithURL:[NSURL URLWithString:new_cover]
                                        placeholderImage:[UIImage imageNamed:@"user_bg"]];
                        
                        //保存login img
                        [[LoginViewController shareUserInfo] setObject:new_cover forKey:@"new_cover"];
                    }
                    
                    [self.tableview reloadData];
                }
            } else {
                [self showAlertView:@"温馨提示" subtitle:[dic objectForKey:@"message"]];
            }
        }
        @catch (NSException *exception) {
            //[self showAlertView:@"温馨提示" subtitle:@"服务器异常！"];
        } @finally {
        }
    } onError:^(NSError *error) {
        [self.view stopLoadingAnimation];
        [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
    } onAnimated:YES];
}

#pragma mark -
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
    UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    {
        NSString * strURL = @"member/upload_avatar";
        if (select_index_picker == 2) {
            //上传背景图片
            strURL = @"member/upload_image";
        }
        
        [self.view startLoadingAnimation];
        [[HttpNewBaseService sharedClient] POST:strURL
                                     parameters:nil
                      constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
        {
            NSMutableString * str_name = nil;
            NSMutableString * str_name_png = nil;
            NSData * data = UIImageJPEGRepresentation(image, 1.0f);
            str_name = [NSMutableString stringWithFormat:@"photo"];
            str_name_png = [NSMutableString stringWithFormat:@"photo.png"];
            [formData appendPartWithFileData:data
                                        name:str_name
                                    fileName:str_name_png
                                    mimeType:@"image/png"];
            
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [self.view stopLoadingAnimation];
            
            NSDictionary * dic = responseObject;
            NSInteger status = [[dic objectForKey:@"status"] integerValue];
            id dic_array = [dic objectForKey:@"data"];
            
            if (status == 1) {
                /*
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"上传成功!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                 */
                
                if (dic_array && [dic_array isKindOfClass:[NSDictionary class]]) {
                    if (select_index_picker==1) {//头像
                        NSString * logo = [dic_array objectForKey:@"src"];
                        if (logo) {
                            NSString * pp = [logo stringByReplacingOccurrencesOfString:@"Attachment/" withString:@""];
                            NSString * new_logo = [NSString stringWithFormat:@"%@%@",JPG,pp];
                            
                            [self.img_header setImageWithURL:[NSURL URLWithString:new_logo]
                                            placeholderImage:nil];
                            
                            //保存login img
                            [[LoginViewController shareUserInfo] setObject:new_logo forKey:@"new_logo"];
                        }
                    }
                    if (select_index_picker==2) {//背景
                        NSString * cover = [dic_array objectForKey:@"src"];
                        if (cover) {
                            NSString * pp = [cover stringByReplacingOccurrencesOfString:@"Attachment/" withString:@""];
                            NSString * new_cover = [NSString stringWithFormat:@"%@%@",JPG,pp];
                            
                            [self.imgProfile setImageWithURL:[NSURL URLWithString:new_cover]
                                            placeholderImage:nil];
                            
                            //保存login img
                            [[LoginViewController shareUserInfo] setObject:new_cover forKey:@"new_cover"];
                        }
                    }
                } else if (dic_array && [dic_array isKindOfClass:[NSString class]]){
                    if (select_index_picker==1) {//头像
                        NSString * logo = dic_array;
                        if (logo) {
                            NSString * pp = [logo stringByReplacingOccurrencesOfString:@"Attachment/" withString:@""];
                            NSString * new_logo = [NSString stringWithFormat:@"%@%@",JPG,pp];
                            
                            [self.img_header setImageWithURL:[NSURL URLWithString:new_logo]
                                            placeholderImage:nil];
                            
                            //保存login img
                            [[LoginViewController shareUserInfo] setObject:new_logo forKey:@"new_logo"];
                        }
                    }
                    if (select_index_picker==2) {//背景
                        NSString * cover = dic_array;
                        if (cover) {
                            NSString * pp = [cover stringByReplacingOccurrencesOfString:@"Attachment/" withString:@""];
                            NSString * new_cover = [NSString stringWithFormat:@"%@%@",JPG,pp];
                            
                            [self.imgProfile setImageWithURL:[NSURL URLWithString:new_cover]
                                            placeholderImage:nil];
                            
                            //保存login img
                            [[LoginViewController shareUserInfo] setObject:new_cover forKey:@"new_cover"];
                        }
                    }
                }
                
            } else {
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:[dic objectForKey:@"message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [self.view stopLoadingAnimation];
            [self showAlertView:@"温馨提示" subtitle:@"上传失败!"];
        }];
    }
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end







@implementation SETableViewCell
{
    UIImageView * line;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        CGFloat width = self.frame.size.width;
        CGFloat height = self.frame.size.height;
        // Initialization code
        if (self.label_title == nil) {
            self.label_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, height)];
            self.label_title.backgroundColor=[UIColor clearColor];
            self.label_title.textColor=[UIColor blackColor];
            self.label_title.font=[UIFont systemFontOfSize:14];
            [self addSubview:self.label_title];
        }
        if (self.text_info == nil) {
            self.text_info = [[UITextField alloc] initWithFrame:CGRectMake(105, 0, width-120, height)];
            self.text_info.backgroundColor=[UIColor clearColor];
            self.text_info.textColor=[UIColor grayColor];
            self.text_info.font = [UIFont systemFontOfSize:14];
            self.text_info.returnKeyType=UIReturnKeyDone;
            self.text_info.userInteractionEnabled=NO;
            [self addSubview:self.text_info];
        }
        
        line = [[UIImageView alloc] initWithFrame:CGRectMake(0, height-1, width, 1.0f)];
        line.image = [UIImage imageNamed:@"line"];
        [self addSubview:line];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (line) {
        CGFloat width = self.frame.size.width;
        CGFloat height = self.frame.size.height;
        
        line.frame=CGRectMake(0, height-1, width, 1.0f);
    }
}

@end











